package com.alpha.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.alpha.bom.ExamResponse;
import com.alpha.dao.ExamResponseDao;

/**
 * Servlet implementation class OngoingExamViewServlet
 */
@WebServlet("/OngoingExamViewServlet")
@ServletSecurity(@HttpConstraint(rolesAllowed = {"admin"}))
public class OngoingExamViewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(OngoingExamViewServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OngoingExamViewServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" get method started.");
		
		int exam_id;
		ArrayList<ExamResponse> examResponses = new ArrayList<ExamResponse>();

		if(request.getParameter("exam_id")!=null){
			exam_id = Integer.parseInt(request.getParameter("exam_id"));
						
			ExamResponseDao rDao = new ExamResponseDao();
			//getting examResponses by exam_id
			try {
				examResponses = rDao.retrieveExamResponseByExam(exam_id);
			} catch (NamingException e) {
				logger.error(e.getMessage());
				return;
			} catch (SQLException e) {
				logger.error(e.getMessage());
				return;
			}
			
			if(examResponses!=null){
				request.setAttribute("examResponses", examResponses);
			}
			
			RequestDispatcher dis=request.getRequestDispatcher("OngoingExamView.jsp");
            dis.forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" post method started.");
		
		int examId = 0;
		
		if(request.getParameter("exam_id") != null) {
			examId = Integer.parseInt(request.getParameter("exam_id"));
		}
		
		ExamResponseDao rDao = new ExamResponseDao();
		try {
			String completeStudents = rDao.retrieveAllCompleteStudents(examId);
			PrintWriter out = response.getWriter();
			out.print(completeStudents);
		} catch (NamingException e) {
			logger.error(e.getMessage());
			return;
		} catch (SQLException e) {
			logger.error(e.getMessage());
			return;
		} 
	}

}
